package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:买牌->洗牌->摸牌
 * User: 石方旭
 * Date: 2022-04-28
 * Time: 11:27
 */
//创建一副牌
class Card {
    private int rank;//扑克牌上的数字
    private String suit;//扑克牌上的花色

    //初始化这个扑克牌
    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "Card{" +
                "rank=" + rank +
                ", suit='" + suit + '\'' +
                '}';
    }
}


public class TestPoker {

    public static final String[] suit = {"♠", "♥", "♣", "♦"};
    public static List<Card> buyCard() {
        List<Card> listCards = new ArrayList<>();
        for(int i=0;i<4;++i) {
            for(int j =1;j<=13;++j) {
                //牌
                Card cards = new Card(j,suit[i]);
                //把牌都存放到数组里
                listCards.add(cards);
            }
        }
        return listCards;
    }
    private static void swap(List<Card> cardList,int i,int j) {
          Card tmp = cardList.get(i);//得到i下标的牌
          cardList.set(i,cardList.get(j));
          cardList.set(j,tmp);
    }

    //开始进行洗牌
    public static void shuffle(List<Card> cardList) {
        for(int i = cardList.size()-1;i>0;--i) {
            Random random = new Random();
            int index = random.nextInt(i);
             swap(cardList,i,index);
        }
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        System.out.println(cardList);
        shuffle(cardList);
        System.out.println(cardList);
        //首先要有三个手，一次摸一张牌，每个人最后一共摸5张牌
        List<Card> hands1 = new ArrayList<>();
        List<Card> hands2 = new ArrayList<>();
        List<Card> hands3 = new ArrayList<>();
        //创建一个二维数组用来存放这三个手
        List<List<Card>> hands = new ArrayList<>();
        hands.add(hands1);
        hands.add(hands2);
        hands.add(hands3);
        for(int i =0; i<5 ; ++i) {
            for(int j=0;j<3;++j) {
                Card cards = cardList.remove(0);//将桌子上的牌移除
                List<Card> cardList1= hands.get(j);
                cardList1.add(cards);
            }
        }
        System.out.println("hands1"+hands1);
        System.out.println("hands2"+hands2);
        System.out.println("hands3"+hands3);
        System.out.println("剩余的牌"+cardList);
    }
}
